//
//  ZWHomeTableViewController.swift
//  张伟的微博
//
//  Created by 伟 on 16/2/26.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit

class ZWHomeTableViewController: ZWBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setnavigationItem()
        
        ZWNetWorwTool.sharedInstance.loatStatus { (reult, error) -> () in
            print("reult:\(reult)")
        }
      
           }
    
    func setnavigationItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_pop")
        
        let button = ZWHomeTitleView(titleName: "张伟微博")
        button.addTarget(self, action: "homeTitleClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = button

    
    }
    
    func homeTitleClick(button:UIButton) {
        button.selected = !button.selected

        
        UIView.animateWithDuration(0.1) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI)-0.0001) : CGAffineTransformIdentity
        }
        if button.selected {
            //加载SB
            let sb = UIStoryboard(name: "popover", bundle: nil)
            //创建控制器
            let popoverVC = sb.instantiateViewControllerWithIdentifier("popover")
            popoverVC.modalPresentationStyle = UIModalPresentationStyle.Popover
            //设置popoverPresentationconteoller的代理
            popoverVC.popoverPresentationController?.delegate = self
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            
            popoverVC.preferredContentSize = CGSize(width: 200, height: 5*44)
            presentViewController(popoverVC,animated: true,completion: nil)
        }
        
        
        
    }

}


extension ZWHomeTableViewController: UIPopoverPresentationControllerDelegate {
    //让IPHONE上显示popover的效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    //popover关闭时调用
    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
        let button = navigationItem.titleView as! ZWHomeTitleView
        homeTitleClick(button)
    }
    
}

